package com.example.homemodule.view.dialog;

import static android.view.Gravity.BOTTOM;

import static com.examination.mlib.constants.ClickApi.Home.click_rebuydoctor;

import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.homemodule.model.bean.PrivateDoctorEntity;
import com.example.homemodule.R;
import com.example.homemodule.adapter.PrivateDoctorAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.yilijk.base.basedialog.BaseDialog;
import com.yilijk.base.basedialog.DialogChain;
import com.yilijk.base.utils.ClickUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by SHICHENG
 * <p>
 * Time on 2022/01/04
 */
public class PrivateDoctor extends BaseDialog implements View.OnClickListener{

    private PrivateDoctorAdapter privateDoctorAdapter;
    private ImageView privateDoctorClose;
    private RecyclerView privateDoctorList;
    private SmartRefreshLayout privateDoctorSmart;
    PrivateDoctorEntity.DataBean data;
    ArrayList<PrivateDoctorEntity.DataBean.MyDoctorVOListBean> strings;
    public PrivateDoctor(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_private_doctor);
        privateDoctorSmart = (SmartRefreshLayout) findViewById(R.id.private_doctor_smart);
        privateDoctorSmart.setEnableRefresh(false);
        privateDoctorClose = (ImageView) findViewById(R.id.private_doctor_close);
        privateDoctorClose.setOnClickListener(this);
        privateDoctorList = (RecyclerView) findViewById(R.id.private_doctor_list);
        strings = new ArrayList<>();
        privateDoctorAdapter = new PrivateDoctorAdapter(strings);
        privateDoctorList.setLayoutManager(new LinearLayoutManager(getContext()));
        privateDoctorList.setAdapter(privateDoctorAdapter);
        privateDoctorAdapter.notifyDataSetChanged();
        ClickUtils.setAdapterItemChildFastOnClickListener(privateDoctorAdapter, new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                ClickUtils.sendEvent(click_rebuydoctor,getContext(),"复诊购药医生复诊点击");
                if (showCallBack != null) {
                    showCallBack.click(strings.get(position).getDoctorId() + "");
                }
            }
        });

        privateDoctorSmart.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                if (showCallBack != null) {
                    showCallBack.getRefresh(privateDoctorSmart);
                }
            }
        });


    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.private_doctor_close){
            dismiss();
        }
    }

    @Override
    public void intercept(@NonNull DialogChain chain) {
        if (chain!=null){
            super.intercept(chain);
        }
    }

    @Override
    public void show() {
        super.show();
        Window window = getWindow();
        window.setGravity(BOTTOM);
        window.setWindowAnimations(R.style.BottomDialogStyle);
        WindowManager windowManager = window.getWindowManager();
        Display defaultDisplay = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.width = (int)(defaultDisplay.getWidth()); //设置宽度
        window.setAttributes(attributes);
        privateDoctorList.scrollToPosition(0);
    }

    public void setData(List<PrivateDoctorEntity.DataBean.MyDoctorVOListBean> data){
        strings.addAll(data);
        privateDoctorAdapter.notifyDataSetChanged();
    }

    public int getDataSize(){
        if (strings == null){
            return 0;
        }
        return strings.size();
    }

    @Override
    public void dismiss() {
        super.dismiss();
        strings.clear();
        if (showCallBack != null){
            showCallBack.onDismiss();
        }
    }

    private ShowCallBack showCallBack;

    public void setShowCallBack(ShowCallBack showCallBack){
        this.showCallBack = showCallBack;
    }

    public interface ShowCallBack{

        void getRefresh(SmartRefreshLayout smartRefreshLayout);

        void click(String doctorId);

        void onDismiss();
    }

}
